extends Node

@export var CardArray = []
@export var CardID = {}
#@export var CardOArray = []
@export var Card:PackedScene

var TEST = []
# Called when the node enters the scene tree for the first time.

func __Create_new_card_name(Name:String,level = 0):
	#var Card = preload("res://pai.tscn")
	var ID = CardArray.size()
	CardArray.append([Name,level])
	CardID[Name] = ID
	
func __Card_new(id:int):
	var TiCard = Card.instantiate()
	TiCard.Cardname = CardArray[id][0]
	TiCard.level = CardArray[id][1]
	return TiCard

func __Card_name_new(Name:String):
	var CardId = CardID[Name]
	if CardId != null:
		return __Card_new(CardId)

func __add_Card_in_box_id(id:int):
	var TiCard = Card.instantiate()
	TiCard.Cardname = CardArray[id][0]
	TiCard.level = CardArray[id][1]
	$HBoxContainer.add_child(TiCard)
	return TiCard
	
func __add_Card_in_box_name(Name:String):
	var CardId = CardID[Name]
	if CardId != null:
		return __add_Card_in_box_id(CardId)
		
func __delete_Card_id(TiCard):
	TiCard.call("__delete_Crad")
# Called every frame. 'delta' is the elapsed time since the previous frame.

func _ready() -> void:
	#$Controlbase/HBoxContainer.separation = 60
	__Create_new_card_name("first")
	__Create_new_card_name("second",1)
	#var C1 = __add_Card_in_box_name("first")
	#TEST.append(C1)
	for i in range(2):
		var cnd = __Card_name_new("first")
		$CardBox.__Add_Card_node(cnd) 
		# TEST.append(cnd)#__add_Card_in_box_name("first"))
	var secd = __Card_name_new("second")
	var CAS:AnimatedSprite2D = secd.get_node("cardA")
	CAS.animation = "sec"
	CAS.frame = 0
	$CardBox.__Add_Card_node(secd)
	# TEST.append(secd)
	#TEST[0].testt = 10
	#print(TEST[1].testt)
	
func _process(delta: float) -> void:
	pass
	if Input.is_action_just_pressed("ui_accept"):
		var secd = __Card_name_new("second")
		var CAS:AnimatedSprite2D = secd.get_node("cardA")
		CAS.animation = "sec"
		CAS.frame = 0
		var ress = $CardBox.__Add_Card_node(secd)
		# if ress:
		# 	TEST.append(secd)
		# else:
		if not ress:
			print("can't put more card!")
	if Input.is_action_just_pressed("ui_focus_next"):
		pass
		# var tcard = $Control/CardBox.__Remove_Card_node(TEST.pop_front())
		# if tcard[0]:
		# 	__delete_Card_id(tcard[1])
	if Input.is_action_just_pressed("ui_cancel"):
		var cnd = __Card_name_new("first")
		var ress = $CardBox.__Add_Card_node(cnd) 
		if not ress:
		# 	TEST.append(cnd)#__add_Card_in_box_name("first"))
		# else:
			print("can't put more card!")
		#print(TEST.size())
	if __BaseFunc.now_draging_node != null:
		__BaseFunc.__putStr_Label($Label,"now :"+__BaseFunc.now_draging_node.tooltip_text)
	else:
		$Label.text = "no"
	$Label2.text = str($CardBox.size) + "\n"+ str($CardBox.position)+"\n"+str($CardBox3.size) +"\n" +str($CardBox3.position)
	# print($Control/CardBox.position,$Control/CardBox3.position,$Control/CardBox4.position)
	#__BaseFunc.__putStr_Label($Label,str(TEST[0].get_screen_position())+"\n"+str(TEST[1].get_screen_position())+"\n"+str(TEST[2].get_screen_position())+"\n"+str(TEST[3].get_screen_position())+"\n")
	#__BaseFunc.__putStr_Label($Label2,str(TEST[0].get_node("AnimationPlayer").get_animation("up").track_get_key_value(0,1))+"\n"+str(TEST[1].get_node("AnimationPlayer").get_animation("up").track_get_key_value(0,1))+"\n"+str(TEST[2].get_node("AnimationPlayer").get_animation("up").track_get_key_value(0,1))+"\n"+str(TEST[3].get_node("AnimationPlayer").get_animation("up").track_get_key_value(0,1)))
		#TEST.pop_back()
